package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.order.bo.WXOrderContentBo;
import com.cskaoyan.bean.order.bo.WXOrderIdBo;
import com.cskaoyan.bean.order.bo.WXOrderSubmitBo;
import com.cskaoyan.bean.order.vo.WXOrderDetailVo;
import com.cskaoyan.bean.order.vo.WXOrderListVo;
import com.cskaoyan.bean.order.vo.WXOrderSubmitVo;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.Subject;

/**
 * @author wangdezhao
 * @date 2021/6/7
 * @description
 */
@RequestMapping("wx/order")
@RestController
public class WXOrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public BaseRespVo list(Integer showType, Integer page, Integer size) {
        User User = (com.cskaoyan.bean.User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = User.getId();
        WXOrderListVo wxOrderListVo = orderService.getWXOrderList(showType, page, size,userId);
        return BaseRespVo.ok(wxOrderListVo);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WXOrderDetailVo wxOrderDetailList = orderService.getWXOrderDetailList(orderId);
        return BaseRespVo.ok(wxOrderDetailList);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody WXOrderIdBo cancelBo){
        int effectedRows = orderService.cancelWXOrder(cancelBo);
        if(effectedRows != 1){
            return BaseRespVo.ok("取消订单失败,请重试");
        }
        return BaseRespVo.ok("成功取消订单");
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody WXOrderIdBo refundBo){
        int effectedRows = orderService.refundWXOrder(refundBo);
        if(effectedRows != 1){
            return BaseRespVo.ok("取消订单失败,请重试");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WXOrderIdBo deleteBo){
        int effectedRows = orderService.deleteWXOrder(deleteBo);
        if(effectedRows != 1){
            return BaseRespVo.ok("取消订单失败,请重试");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody WXOrderIdBo confirmBo){

        int effectedRows =  orderService.confirmWXOrder(confirmBo);;
        if(effectedRows != 1){
            return BaseRespVo.ok("取消订单失败,请重试");
        }
        return BaseRespVo.ok("成功");
    }


    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId){
        OrderGoods wxGoods = orderService.getWXGoods(orderId, goodsId);
        return BaseRespVo.ok(wxGoods);
    }

    @RequestMapping("comment")
    public BaseRespVo content(@RequestBody WXOrderContentBo contentBo){
        int effectedRows  = orderService.addWXContent(contentBo);
        if(effectedRows != 1){
            return BaseRespVo.ok("取消订单失败,请重试");
        }
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WXOrderSubmitBo submitBo){
        int id = orderService.submitWXOrder(submitBo);

        WXOrderSubmitVo wxOrderSubmitVo = new WXOrderSubmitVo(id);
        return BaseRespVo.ok(wxOrderSubmitVo);
    }

}
